
$(document).ready(function() {
    initPage();

    $("#submit-btn").click(function () {
        if(isEmpty($("#username").val())){
            setWarnMessage($("#username"),"请输入用户名");
        }
        if(isEmpty($("#email").val())){
            setWarnMessage($("#email"),"请输入邮箱地址");
        }
        if(isEmpty($("#name").val())){
            setWarnMessage($("#name"),"请输入姓名");
        }
        if(isEmpty($("#password").val())){
            setWarnMessage($("#password"),"请输入密码");
        }
        var form = $("#register-form").find("span.help-block") ;
        if(form.length > 0){
            return;
        }else{
            $("#register-form").submit();
        }
    })
});

/**
 * 验证字符串是否为空
 * @param Str
 * @returns {boolean}
 */
function isEmpty(Str) {
    if(Str == null || Str ==''){
        return true;
    }
    return false;
}

/**
 * 设置警告信息
 * @param obj
 * @param message
 */
function setWarnMessage(obj,message){
    if(obj.next().hasClass("help-block")){
        obj.next().text(message);
    }else{
        obj.parent().addClass("has-warning").append("<span class='help-block text-danger'>"+ message +"</span>")
    }
}

/**
 * 移除警告信息
 * @param obj
 */
function removeWarnMessage(obj) {
    if(obj.next().hasClass("help-block")){
        obj.parent().removeClass("has-warning");
        obj.next().remove();
    }
}

/**
 * 验证邮箱
 * @param value
 * @param alerttxt
 * @returns {boolean}
 */
function validate_email(value) {
    apos=value.indexOf("@")
    dotpos=value.lastIndexOf(".")
    if (apos<1||dotpos-apos<2) {
        return false
    }
    else {
        return true;
    }
}

/**
 * 初始化页面
 */
function initPage(){

    $("#username").blur(function () {
        var obj = $(this);
        var username = this.value;
        if(isEmpty(username)){
            setWarnMessage(obj,"请输入用户名");
            return;
        }else if (username.length <3){
            setWarnMessage(obj,"用户名不能小于3个字符");
            return;
        }else{
            $.ajax({
                url: "/blog/users/validUsername?username=" + username,
                success: function(data){
                    if(data.success == false){
                        setWarnMessage(obj,data.message);
                    }else {
                        removeWarnMessage(obj);
                    }
                }
            });
        }
    });

    $("#email").blur(function () {
        var obj = $(this);
        var email = this.value;
        if(isEmpty(email)){
            setWarnMessage(obj,"请输入邮箱地址")
            return
        }else if(!validate_email(email)){
            setWarnMessage(obj,"请输入正确的邮箱地址")
            return
        }else{
            $.ajax({
                url: "/blog/users/validEmail?email=" + email,
                success: function(data){
                    if(data.success == false){
                        setWarnMessage(obj,data.message);
                    }else {
                        removeWarnMessage(obj);
                    }
                }
            });
        }
    });

    $("#name").blur(function () {
        var obj = $(this);
        var name = this.value;
        if(isEmpty(name)){
            setWarnMessage(obj,"请输入姓名")
            return
        }else if(name.length < 2){
            setWarnMessage(obj,"姓名不能小于2个字符")
            return
        }else{
            removeWarnMessage(obj);
        }
    });

    $("#password").blur(function () {
        var obj = $(this);
        var password = this.value;
        if(isEmpty(password)){
            setWarnMessage(obj,"请输入密码")
            return
        }else if(password.length < 6){
            setWarnMessage(obj,"密码不能小于6个字符")
            return
        }else{
            removeWarnMessage(obj);
        }
    })
}